<template>
  <div class="login">
    <Header title="登录"></Header>
    <div class="img">美团</div>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{required: true, message: '请填写用户名'}]"
      >
      </van-field>
      <van-field
          v-model="password"
          name="password"
          label="密码"
          placeholder="密码"
          type="password"
          :rules="[{required: true, message: '请填写密码'}]"
      >
      </van-field>

      <div style="margin-top: 16px;">
        <van-button round block type="info" native-type="submit" color="#ffc400">登录</van-button>
        <van-button round block type="info" color="#ffc400"
          class="register" @click="register">去注册</van-button>
      </div>
    </van-form>
  </div>
</template>

<script setup>
import Header from '@/components/Header'
import {ref} from "vue";
import {useRouter} from "vue-router";
import {Toast} from "vant";

const username = ref('admin')
const password = ref('1234')

const router = useRouter()
// 登录
const onSubmit = (value) => {
  if(!localStorage.userInfo){
    Toast('账户未注册')
    return
  }else{
    let userInfo = JSON.parse(localStorage.getItem('userInfo'))
    if(userInfo['username'] === value['username']){
      if(userInfo['password'] === value['password']){
        Toast('登录成功')
        localStorage.setItem('isLogin', 'login')
        router.push('/home')
      }else{
        Toast('密码错误')
      }
    }else{
      Toast('账户不存在')
    }
  }
}
const register = () => {
  router.push('/register')
}
</script>

<style lang="less" scoped>
.login {
  .img {
    width: 200px;
    height: 200px;
    background-color: #ffc400;
    font-size: 80px;
    line-height: 200px;
    text-align: center;
    border-radius: 40px;
    margin: 20px auto;
  }
  .register {
    margin-top: 20px;
  }
}
</style>